//
//  LoginController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/3.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "LoginController.h"

@interface LoginController ()<SelectBtnsDelegate>
{
    SelectBtns *selectBrn;
    UILabel *loginWayLabel;
    UILabel *passwordLabel;
    LineTextField *userIDTf;
    LineTextField*passwordTf;
    
    UIButton *loginBtn;
    
    UIButton *forgetPasswordBtn;
    
    UIButton *goRegistBtn;
    NSMutableDictionary *loginDic;
}



@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    loginDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"1"]  forKey:@"type"];
    [self addSelectRegistrationBts];
    [self addUserIDSubViews];
    [self addPasswordSubViews];
    [self addLoginBtnSubViews];
}
- (void)addSelectRegistrationBts{
    selectBrn= [[SelectBtns alloc] initWithFrame:CGRectMake(kWidth(40), kHeight(235), kWidth(700), kHeight(70))];
    selectBrn.delegate = self;
    [selectBrn setBtntitleWithArray:@[StringWithKey(@"LgVC0"), StringWithKey(@"LgVC1")]];
    [self.view addSubview:selectBrn];
}
///注册方式label
- (void)addUserIDSubViews{
    
    loginWayLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(selectBrn.frame) + kHeight(90), kWidth(680), kHeight(60))];
    [self.view addSubview:loginWayLabel];
    loginWayLabel.text = StringWithKey(@"LgVC2");
    loginWayLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    loginWayLabel.textAlignment = NSTextAlignmentLeft;
    loginWayLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    userIDTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(loginWayLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(130))];
    
    [userIDTf setTextConentStyleWith:TextWayEmail];
    [self.view addSubview:userIDTf];
    
    
}
///密码
- (void)addPasswordSubViews{
    passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60), kWidth(380), kHeight(60))];
    [self.view addSubview:passwordLabel];
    passwordLabel.text = StringWithKey(@"LgVC3");
    passwordLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    passwordLabel.textAlignment = NSTextAlignmentLeft;
    passwordLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    passwordTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(135))];
    
    [passwordTf setTextConentStyleWith:TextWayPassWord];
    [self.view addSubview:passwordTf];

}
///注册按钮已经协议
- (void)addLoginBtnSubViews{
    CGSize forgetPasswordBtnSize = [self labelAutoCalculateRectWith:StringWithKey(@"LgVC4") FontSize:kHeight(44) MaxSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    forgetPasswordBtn  = [self setBtnWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(45), forgetPasswordBtnSize.width, kHeight(90)) title:StringWithKey(@"LgVC4")];
    [forgetPasswordBtn setTitleColor:[UIColor colorWithHexString:@"#40b5f8"] forState:UIControlStateNormal];
    forgetPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:kHeight(44)];
    [self.view addSubview:forgetPasswordBtn];
    [forgetPasswordBtn addTarget:self action:@selector(handleForgetPasswordBtn) forControlEvents:UIControlEventTouchUpInside];
    
    loginBtn = [self setBtnWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(forgetPasswordBtn.frame) + kHeight(45), ScreenWidth-kWidth(180), kHeight(150)) title:StringWithKey(@"LRVC0")];
    loginBtn.backgroundColor = [UIColor colorWithHexString:@"#40b5f8"];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:loginBtn];
    [loginBtn addTarget:self action:@selector(handleloginBtn) forControlEvents:UIControlEventTouchUpInside];
    CGSize goRegistBtnSize = [self labelAutoCalculateRectWith:StringWithKey(@"LgVC5") FontSize:kHeight(44) MaxSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    goRegistBtn =  [self setBtnWithFrame:CGRectMake((ScreenWidth - goRegistBtnSize.width)/2, CGRectGetMaxY(loginBtn.frame) + kHeight(35), goRegistBtnSize.width, kHeight(150)) title:StringWithKey(@"LgVC5")];
    [goRegistBtn setTitleColor:[UIColor colorWithHexString:@"#40b5f8"] forState:UIControlStateNormal];
    goRegistBtn.titleLabel.font = [UIFont systemFontOfSize:kHeight(44)];
    [self.view addSubview:goRegistBtn];
    [goRegistBtn addTarget:self action:@selector(handleGoRegist) forControlEvents:UIControlEventTouchUpInside];
    
}
- (UIButton *)setBtnWithFrame:(CGRect) frame title:(NSString *)title{
    UIButton *abutton = [UIButton buttonWithType:UIButtonTypeCustom];
    abutton.frame = frame;
    [abutton setTitle:title forState:UIControlStateNormal];
    abutton.layer.cornerRadius = frame.size.height/2;
    abutton.layer.masksToBounds = YES;
    
    return abutton;
}
- (CGSize)labelAutoCalculateRectWith:(NSString *)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize

{
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary * attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    
    
    CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    
    
    labelSize.height = ceil(labelSize.height);
    
    labelSize.width = ceil(labelSize.width);
    
    return labelSize;
    
}
#pragma mark -- SelectBtnsDelegate
- (void)selectLeftBtn{
    [self tfEesignFirstResponder];
    
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"1"] forKey:@"type"];
    loginWayLabel.text = StringWithKey(@"LgVC2");
    [userIDTf setTextConentStyleWith:TextWayEmail];
}  
- (void)selectRightBtn{
    [self tfEesignFirstResponder];
    
    [loginDic setValue:[NSString enCryptKey:postKey strPassword:@"2"] forKey:@"type"];
    loginWayLabel.text = StringWithKey(@"LgVC6");
    [userIDTf setTextConentStyleWith:TextWayIphoneNumber];
}
- (void)tfEesignFirstResponder{
    passwordTf.textField.text = @"";
    [userIDTf.textField resignFirstResponder];
    [passwordTf.textField  resignFirstResponder];
}
#pragma mark --
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![userIDTf.textField isExclusiveTouch] || ![passwordTf.textField isExclusiveTouch] ) {
        [userIDTf.textField resignFirstResponder];
        [passwordTf.textField resignFirstResponder];
    }
}
- (void)handleloginBtn{
    
//    [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:ISLogin];
//    [UIApplication sharedApplication].keyWindow.rootViewController = [[RootController alloc]init];
    
    
    NSString *sty = [loginDic valueForKey:@"type"];
    
    [userIDTf resignFirstResponder];
    if (userIDTf.returnRight == YES ) {
         [passwordTf resignFirstResponder];
    if (passwordTf.returnRight == YES) {
        [userIDTf.textField resignFirstResponder];
        [passwordTf.textField resignFirstResponder];
    if ([sty isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
        
//        [loginDic setValue:[NSString enCryptKey:postKey strPassword:sty] forKey:@"type"];
        
                
            
            ///邮箱登录
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"email"];
            [loginDic setValue:[NSString enCryptKey:postKey strPassword:passwordTf.textField.text] forKey:@"password"];
            [loginDic setValue:@"" forKey:@"phone"];
            [loginDic setValue:@"" forKey:@"code"];
       
        
    }else{
        ///手机号登录
//        [loginDic setValue:[NSString enCryptKey:postKey strPassword:sty] forKey:@"type"];
        [loginDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
        [loginDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"phone"];
        [loginDic setValue:[NSString enCryptKey:postKey strPassword:passwordTf.textField.text] forKey:@"password"];
        [loginDic setValue:@"" forKey:@"email"];
        
    }
    
    }else{
        return;
    }
//    else{
//             AlterView(StringWithKey(@"ALter0"), StringWithKey(@"ALter9"), StringWithKey(@"ALter8"));
//             return;
//         }
    }else{
        return;
    }
    NSLog(@"%@", loginDic);
    [GLHttpTool post:[NSString stringWithFormat:@"%@/user/login", UrlAddress] params:loginDic success:^(id responseObj) {
        NSLog(@"responseObj==%@", responseObj);
        NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
        NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
        if ([code isEqualToString:@"200"]) {
            [[NSUserDefaults standardUserDefaults] setValue:loginDic[@"type"] forKey:isLogInEmail];
            if ([loginDic[@"type"] isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
                [[NSUserDefaults standardUserDefaults] setValue:userIDTf.textField.text forKey:EmailAdd];
            }else{
                [[NSUserDefaults standardUserDefaults] setValue:userIDTf.textField.text forKey:PhoneNum];
            }
            UserInfo *user = [UserInfo sharedUserInfo];
            [user setInfoWithJsonData:responseObj];
            [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:ISLogin];
            [[NSUserDefaults standardUserDefaults] setValue:passwordTf.textField.text forKey:PassWord];
            [[NSUserDefaults standardUserDefaults] setValue:user.email forKey:EmailAdd];
            [[NSUserDefaults standardUserDefaults] setValue:user.phone forKey:PhoneNum];
            [[NSUserDefaults standardUserDefaults] setValue:[UserInfo sharedUserInfo].unique_id forKey:Unique_id];
            
            [UIApplication sharedApplication].keyWindow.rootViewController = [[RootController alloc]init];
        }else{
            
            AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
            
        }
    } failure:^(NSError *error) {
        NSLog(@"error==%@", error);
    }];
    
}
- (void)handleForgetPasswordBtn{
    ForgetPassWordViewController *forgetVc = [[ForgetPassWordViewController alloc] init];
    [self.navigationController pushViewController:forgetVc animated:YES];
}
- (void)handleGoRegist{
    RegistController *registVC = [[RegistController alloc] init];
    [self.navigationController pushViewController:registVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
